<?php
namespace Admin\Controller;
use Think\Log;
class AdminController extends BaseController
{


    public function user()
    {
        $num = 25;
        $p = I("get.p") ? I("get.p") : 1;
        cookie("prevUrl", "Admin/Admin/user/p/" . $p);

        $userList = D("Admin")->getUserList(array(), true, "id desc", $p, $num);
        $this->assign("userList", $userList);// 赋值数据集

        $count = D("Admin")->getUserListCount();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig("theme", "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show();// 分页显示输出

        $this->assign("page", $show);// 赋值分页输出
        $this->display();
    }

    public function login()
    {
        if (IS_POST) {


            $where = array();
            $where ["username"] = I("post.username");
            $where ["password"] = md5(I("post.password"));

            Log::write('==============pwd=='.md5(I("post.password")));
            $user = D("Admin")->getUser($where, true);

            if ($user) {

                session("adminName", $user["username"]);
                session("adminId", $user["id"]);
                session("adminGroupId", $user["group_id"]);
                $this->redirect("Admin/Index/index");
            } else {
                $this->redirect("Admin/Admin/login");
            }
        } else {
            $arr = array(4, 5, 7, 10, 11, 12);
            $get = $arr[mt_rand(0, count($arr) - 1)];
            $wallpaper = __ROOT__ . "/Public/WallPage/" . $get . ".jpg";
            Log::write('==============wallpaper=='.$wallpaper);
            $this->assign("wallpaper", $wallpaper);
            $this->display();
        }
    }

    public function getVerify()
    {
        $config = array(
            "fontSize" => 30,    // 验证码字体大小
            "length" => 4,     // 验证码位数
            "useNoise" => true, // 关闭验证码杂点
            "useCurve" => false, // 关闭验证码杂点
            "codeSet" => "0123456789",
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    public function check_verify($code, $id = "")
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function logout()
    {
        parent::logout();
        $this->redirect("Admin/User/login");
    }

    public function delUser()
    {
        D("User")->delUser(I("get.id"));

        $this->success("删除成功", cookie("prevUrl"));
    }

    public function addUser()
    {
        if (IS_POST) {
            $data = I("post.");
            if ($data["password"] != "") {
                Log::write('==============pwd=='.$data["password"]);
                $data["password"] = md5($data["password"]);
            } else {
                unset($data["password"]);
            }

            if ($data["group_id"] > 0) {
                D("AuthGroupAccess")->addAccess($data["id"], $data["group_id"]);
            } else {
                D("AuthGroupAccess")->delAccess($data["id"]);
            }
            unset($data["group_id"]);
            if ($data["id"] == 0) {
                D("Admin")->addUser($data);
            } else {
                D("Admin")->saveUser($data);
            }

            $this->success("添加成功", cookie("prevUrl"));
        } else {
            $authGroupList = D("AuthGroup")->getAuthGroupList();
            $this->assign("authGroupList", $authGroupList);
            $this->display();
        }
    }

    public function modifyUser()
    {
        $user = D("Admin")->getUser(array("id" => I("get.id")),true);
        $this->assign("user", $user);

        $authGroupList = D("AuthGroup")->getAuthGroupList();
        $this->assign("authGroupList", $authGroupList);

        $this->display("Admin:addUser");
    }

    public function authGroup()
    {
        $authGroupList = D("AuthGroup")->getAuthGroupList();
        $this->assign("authGroupList", $authGroupList);
        $this->display();
    }

    public function addAuthGroup()
    {
        if (IS_POST) {
            if (!I("post.rules")) {
                $this->error("权限不能为空", "Admin/Admin/addAuthGroup");
            }

            D("AuthGroup")->addAuthGroup(I("post."));
            $this->success("添加成功", "Admin/Admin/authGroup");
        } else {
            $authRuleList = D("AuthRule")->getAuthRuleList();
            $this->assign("authRuleList", $authRuleList);
            $this->display();
        }
    }

    public function modifyAuthGroup()
    {
        $authGroup = D("AuthGroup")->getAuthGroup(I("get.id"));
        $this->assign("authGroup", $authGroup);

        $authRuleList = D("AuthRule")->getAuthRuleList();
        $this->assign("authRuleList", $authRuleList);

        $this->display("Admin:addAuthGroup");
    }

    public function authRule()
    {
        $authRuleList = D("AuthRule")->getAuthRuleList();
        $this->assign("authRuleList", $authRuleList);
        $this->display();
    }

    public function delAuthGroup()
    {
        D("AuthGroup")->delAuthGroup(I("get.id"));

        $this->success("删除成功", "Admin/Admin/authGroup");
    }
}